package com.teama4.shop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.teama4.shop.domain.CartSpeCom;
import com.teama4.shop.domain.Commodity;
import com.teama4.shop.domain.CommodityExample;
import com.teama4.shop.domain.User;
import com.teama4.shop.service.CartService;
import com.teama4.shop.service.CommodityService;
import com.teama4.shop.service.UserService;
import com.teama4.shop.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Controller

public class IndexController {
    @Autowired
    private UserService userService;

    @Autowired
    private CommodityService commodityService;

    @RequestMapping("/indexRecommmend")
    public void indexRecommmend(HttpServletRequest req, HttpServletResponse resp, Integer pageNum) {
        //查询推荐信息
        long total=0;
        List<Commodity> commodities = new ArrayList<>();
        //判断是否登录
        HttpSession session = req.getSession(false);
        if (session != null && session.getAttribute("userId") != null) {
            Integer userId = (Integer) session.getAttribute("userId");
            User user = userService.selectByPrimaryKey(userId);
            String RGStr = user.getRecommendedGoods();
            RGStr = RGStr.substring(0,RGStr.length()-1);
            String[] RGSs = RGStr.split(",");
            List<Integer> RGSList = new ArrayList<>();
            for (int i=0;i<RGSs.length;i++){
                RGSList.add(Integer.valueOf(RGSs[i]));
            }
            CommodityExample commodityExample = new CommodityExample();
            commodityExample.createCriteria().andIdIn(RGSList);
            if(pageNum>4)
                pageNum=1;
            PageHelper.startPage(pageNum,5);
            commodities= commodityService.selectByExample(commodityExample);
            total = ((Page) commodities).getTotal();
            if (commodities.size()<5){
                PageHelper.startPage(1,5-commodities.size());
                List<Commodity> commodities1 = commodityService.selectByExample(commodityExample);
                for(int i=0;i<commodities1.size();i++){
                    commodities.add(commodities1.get(i));
                }


            }
        }else {
            CommodityExample commodityExample = new CommodityExample();
            commodityExample.createCriteria().andBayCountGreaterThanOrEqualTo(30)
                    .andViewCountGreaterThanOrEqualTo(30)
                    .andComPriceGreaterThanOrEqualTo    (20.00);
            PageHelper.startPage(pageNum,5);
            commodities= commodityService.selectByExample(commodityExample);
            total = ((Page) commodities).getTotal();
        }



        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("commodities", commodities);
        map.put("pageNum", pageNum);
        JSONUtil.printByJSON(resp, map);
    }

    @RequestMapping("/")
    public String index(HttpServletRequest req, Model model) {
        //查询推荐信息
        List<Commodity> commodities = new ArrayList<>();
        //判断是否登录
        HttpSession session = req.getSession(false);

        if (session != null && session.getAttribute("userId") != null) {
            Integer userId = (Integer) session.getAttribute("userId");
            User user = userService.selectByPrimaryKey(userId);
            String RGStr = user.getRecommendedGoods();
            if(RGStr!=""){
                RGStr = RGStr.substring(0,RGStr.length()-1);
                String[] RGSs = RGStr.split(",");
                List<Integer> RGSList = new ArrayList<>();
                for (int i=0;i<RGSs.length;i++){
                    RGSList.add(Integer.valueOf(RGSs[i]));
                }
                CommodityExample commodityExample = new CommodityExample();
                commodityExample.createCriteria().andIdIn(RGSList);
                PageHelper.startPage(1,5);
                commodities= commodityService.selectByExample(commodityExample);
            }else {
                CommodityExample commodityExample = new CommodityExample();
                commodityExample.createCriteria().andBayCountGreaterThanOrEqualTo(30)
                        .andViewCountGreaterThanOrEqualTo(30)
                        .andComPriceGreaterThanOrEqualTo(20.00);
                PageHelper.startPage(1,5);
                commodities= commodityService.selectByExample(commodityExample);
            }

        }else {
            CommodityExample commodityExample = new CommodityExample();
            commodityExample.createCriteria().andBayCountGreaterThanOrEqualTo(30)
                    .andViewCountGreaterThanOrEqualTo(30)
                    .andComPriceGreaterThanOrEqualTo(20.00);
            PageHelper.startPage(1,5);
            commodities= commodityService.selectByExample(commodityExample);
        }

        model.addAttribute("commodities",commodities);
        model.addAttribute("pageNum",1);

        return "index";
    }
}
